package com.lagou.test04.test05;

import java.io.*;
import java.net.Socket;
import java.util.List;

public class FileSocketThread extends Thread {

    private Socket socket;
    private List<Socket> socketList;

    public FileSocketThread(Socket socket, List<Socket> socketList) {
        this.socket = socket;
        this.socketList = socketList;
    }

    @Override
    public void run() {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            //创建文件输入流
            bis = new BufferedInputStream(socket.getInputStream());
            //创建输出流
            bos = new BufferedOutputStream(new FileOutputStream("targetPath"));
            //创建缓冲区
            byte[] bArr = new byte[1024];
            int res = 0;
            //读取文件并写入
            while ((res = bis.read(bArr)) != -1) {
                bos.write(bArr, 0, res);
            }
            System.out.println("线程" + Thread.currentThread().getName() + "拷贝成功！");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
